﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace NCS_CS
{
    static class SomeUtils
    {
        public static Bitmap GetWindowScreenshot(IntPtr WndHandle)
        {

            //IntPtr hDCSrc;// As Long
            //IntPtr hDCMemory;// As Long
            //IntPtr hBmp;// As Long
            //IntPtr hBmpPrev;// As Long
            //int WidthSrc;// As Long
            //int HeightSrc;// As Long
            //RECT rc = new RECT();// As RECT
            //GetWindowRect((IntPtr)WndHandle,ref rc);
            ////Marshal.PtrToStructure(p1, typeof(RECT));
            //WidthSrc = rc.Right - rc.Left;
            //HeightSrc = rc.Bottom - rc.Top;

            ////Get Window  device context
            //hDCSrc = GetWindowDC((IntPtr)WndHandle);

            ////create a memory device context
            //hDCMemory = CreateCompatibleDC((IntPtr)hDCSrc);

            ////create a bitmap compatible with window hdc
            //hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc);
            ////Bitmap Pic = new Bitmap(WidthSrc, HeightSrc);
            ////hBmp=Pic.GetHbitmap();
            ////copy newly created bitmap into memory device context
            //hBmpPrev = SelectObject(hDCMemory, hBmp);


            ////copy window window hdc to memory hdc
            //BitBlt(hBmp, 0, 0, WidthSrc, HeightSrc, hDCSrc, 0, 0, 0xCC0020);//vbSrcCopy);

            ////Get Bmp from memory Dc
            //hBmp = SelectObject(hDCMemory, hBmpPrev);

            ////release the created objects and free memory
            //DeleteDC(hDCMemory);
            //ReleaseDC((IntPtr)WndHandle, hDCSrc);

            return null;
        }
        public static int ConvertTwipsToPixels(int twips, MeasureDirection direction)
        {
            return (int)(((double)twips) * ((double)GetPPI(direction)) / 1440.0);
        }

        public static int ConvertPixelsToTwips(int pixels, MeasureDirection direction)
        {
            return (int)((((double)pixels) * 1440.0) / ((double)GetPPI(direction)));
        }

        public static int GetPPI(MeasureDirection direction)
        {
            int ppi;
            IntPtr dc = GetDC(IntPtr.Zero);

            if (direction == MeasureDirection.Horizontal)
                ppi = GetDeviceCaps(dc, DeviceCap.LOGPIXELSX); //DEVICECAP LOGPIXELSX
            else
                ppi = GetDeviceCaps(dc, DeviceCap.LOGPIXELSY); //DEVICECAP LOGPIXELSY

            ReleaseDC(IntPtr.Zero, dc);
            return ppi;
        }
        //Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
        //Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
        //Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        //Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int w, int h);
        //Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
        [DllImport("gdi32.dll")]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
        //Private Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long
        [DllImport("gdi32.dll")]
        public static extern IntPtr BitBlt(IntPtr hdcdest, int xdest, int ydest, int w, int h, IntPtr hdcSrc, int xsrc, int ysrc, int drop);
        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT r);

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hWnd);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteDC(IntPtr hdc);
        [DllImport("user32.dll")]
        static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, DeviceCap devCap);
        [StructLayout(LayoutKind.Sequential)]
        internal struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        [StructLayout(LayoutKind.Sequential)]
        internal struct CHARRANGE
        {
            public int cpMin;
            public int cpMax;
        }
        [StructLayout(LayoutKind.Sequential)]
        internal struct FORMATRANGE
        {
            public IntPtr hdc;             //渲染设备
            public IntPtr hdcTarget;       //目标设备
            public RECT rc;           //渲染区域，单位：缇。
            public RECT rcPage;          //渲染设备的整体区域，单位：缇。
            public CHARRANGE chrg;      //用于格式化的文本范围。
        }
        struct PageInfo
        {
            public int PageNumber;// As Long          '页码
            public int Start;// As Long                   '字符起始位置
            public int End;//As Long                     '字符终止位置
            public int ActualHeight;// As Long            '本页实际打印高度
        }
    }

    public enum MeasureDirection
    {
        Horizontal,
        Vertical
    }
    public enum DeviceCap
    {
        /// <summary>
        /// Device driver version
        /// </summary>
        DRIVERVERSION = 0,
        /// <summary>
        /// Device classification
        /// </summary>
        TECHNOLOGY = 2,
        /// <summary>
        /// Horizontal size in millimeters
        /// </summary>
        HORZSIZE = 4,
        /// <summary>
        /// Vertical size in millimeters
        /// </summary>
        VERTSIZE = 6,
        /// <summary>
        /// Horizontal width in pixels
        /// </summary>
        HORZRES = 8,
        /// <summary>
        /// Vertical height in pixels
        /// </summary>
        VERTRES = 10,
        /// <summary>
        /// Number of bits per pixel
        /// </summary>
        BITSPIXEL = 12,
        /// <summary>
        /// Number of planes
        /// </summary>
        PLANES = 14,
        /// <summary>
        /// Number of brushes the device has
        /// </summary>
        NUMBRUSHES = 16,
        /// <summary>
        /// Number of pens the device has
        /// </summary>
        NUMPENS = 18,
        /// <summary>
        /// Number of markers the device has
        /// </summary>
        NUMMARKERS = 20,
        /// <summary>
        /// Number of fonts the device has
        /// </summary>
        NUMFONTS = 22,
        /// <summary>
        /// Number of colors the device supports
        /// </summary>
        NUMCOLORS = 24,
        /// <summary>
        /// Size required for device descriptor
        /// </summary>
        PDEVICESIZE = 26,
        /// <summary>
        /// Curve capabilities
        /// </summary>
        CURVECAPS = 28,
        /// <summary>
        /// Line capabilities
        /// </summary>
        LINECAPS = 30,
        /// <summary>
        /// Polygonal capabilities
        /// </summary>
        POLYGONALCAPS = 32,
        /// <summary>
        /// Text capabilities
        /// </summary>
        TEXTCAPS = 34,
        /// <summary>
        /// Clipping capabilities
        /// </summary>
        CLIPCAPS = 36,
        /// <summary>
        /// Bitblt capabilities
        /// </summary>
        RASTERCAPS = 38,
        /// <summary>
        /// Length of the X leg
        /// </summary>
        ASPECTX = 40,
        /// <summary>
        /// Length of the Y leg
        /// </summary>
        ASPECTY = 42,
        /// <summary>
        /// Length of the hypotenuse
        /// </summary>
        ASPECTXY = 44,
        /// <summary>
        /// Shading and Blending caps
        /// </summary>
        SHADEBLENDCAPS = 45,

        /// <summary>
        /// Logical pixels inch in X
        /// </summary>
        LOGPIXELSX = 88,
        /// <summary>
        /// Logical pixels inch in Y
        /// </summary>
        LOGPIXELSY = 90,

        /// <summary>
        /// Number of entries in physical palette
        /// </summary>
        SIZEPALETTE = 104,
        /// <summary>
        /// Number of reserved entries in palette
        /// </summary>
        NUMRESERVED = 106,
        /// <summary>
        /// Actual color resolution
        /// </summary>
        COLORRES = 108,

        // Printing related DeviceCaps. These replace the appropriate Escapes
        /// <summary>
        /// Physical Width in device units
        /// </summary>
        PHYSICALWIDTH = 110,
        /// <summary>
        /// Physical Height in device units
        /// </summary>
        PHYSICALHEIGHT = 111,
        /// <summary>
        /// Physical Printable Area x margin
        /// </summary>
        PHYSICALOFFSETX = 112,
        /// <summary>
        /// Physical Printable Area y margin
        /// </summary>
        PHYSICALOFFSETY = 113,
        /// <summary>
        /// Scaling factor x
        /// </summary>
        SCALINGFACTORX = 114,
        /// <summary>
        /// Scaling factor y
        /// </summary>
        SCALINGFACTORY = 115,

        /// <summary>
        /// Current vertical refresh rate of the display device (for displays only) in Hz
        /// </summary>
        VREFRESH = 116,
        /// <summary>
        /// Horizontal width of entire desktop in pixels
        /// </summary>
        DESKTOPVERTRES = 117,
        /// <summary>
        /// Vertical height of entire desktop in pixels
        /// </summary>
        DESKTOPHORZRES = 118,
        /// <summary>
        /// Preferred blt alignment
        /// </summary>
        BLTALIGNMENT = 119
    }
}
